package com.leanyuan.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.main.domain.RepForm;
import com.leanyuan.main.domain.RepReg;
import com.leanyuan.main.service.RepFormService_M;
import com.leanyuan.main.service.RepRegService_M;
import com.leanyuan.main.util.RandomUtil;
import com.leanyuan.main.util.RepRegDto;
import com.leanyuan.main.util.RepformVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 相亲相爱一家人
 * @since 2021-05-25
 */
@RestController
@RequestMapping("/leanyuan/rep-reg")
public class RepRegController {
    @Autowired
    private RepFormService_M repFormServiceM;
    @Autowired
    private RepRegService_M repRegServiceM;
    //显示报修设置信息
    @RequestMapping("findReg")
    public RepformVo findReg(@RequestBody RepRegDto repRegDto) {
        System.out.println("********************");
        Page<RepReg> page = new Page<>(repRegDto.getPageNum(),repRegDto.getPageSize());
        System.out.println(repRegDto);
        Page<RepReg> pageData = repRegServiceM.findReg$FormName(page,repRegDto);
        return RepformVo.success(pageData);
    }

    //显示未处理的报修单供登记
    @GetMapping("findContent")
    public RepformVo findContent() {
        QueryWrapper<RepForm> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time").eq("is_accepted","未处理");
        List<RepForm> list = repFormServiceM.list(wrapper);
        return RepformVo.success(list);
    }

    //添加工单
    @PostMapping("addReg")
    public RepformVo addReg(@RequestBody RepReg repReg) {
        QueryWrapper<RepForm> wrapper = new QueryWrapper<>();
        wrapper.eq("form_id",repReg.getFormId());
        RepForm repForm = repFormServiceM.getById(repReg.getFormId());
        repForm.setIsAccepted("已处理");
        repFormServiceM.update(repForm,wrapper);
        repReg.setWorkId(RandomUtil.getRandomFileName());

        repRegServiceM.save(repReg);
        return RepformVo.success(null);
    }

    //选择要处理的报修单后自动显示报修单地址
    @GetMapping("findAdr")
    public RepformVo findAdr(Long id) {
        RepForm byId = repFormServiceM.getById(id);
        return RepformVo.success(byId);
    }

    //修改
    @PostMapping("updReg")
    public RepformVo updReg(@RequestBody RepReg repReg) {
        QueryWrapper<RepReg> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id",repReg.getWorkId());
        repRegServiceM.update(repReg,wrapper);
        return RepformVo.success(null);
    }

    //删除
    @DeleteMapping("delete/{id}")
    public RepformVo deleteReg(@PathVariable(name = "id")String id) {
        QueryWrapper<RepReg> wrapper = new QueryWrapper<>();
        wrapper.eq("work_id",id);
        RepReg reg = repRegServiceM.getById(id);

        QueryWrapper<RepForm> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("form_id",reg.getFormId());
        RepForm repForm = repFormServiceM.getById(reg.getFormId());
        repForm.setIsAccepted("未处理");
        System.out.println(repForm);
        repFormServiceM.update(repForm,wrapper2);
        repRegServiceM.removeById(id);
        return RepformVo.success(null);
    }
}

